define(function(require){
    var $ = require('jquery');
    require('./jquery.extend');
    var pageTpl = $('#pageTpl').html();

    var Portal = function(options){
        this.rootNode = options.rootNode;
        this.id = 0;
        this.pagesNode = this.rootNode.find('.pages');
        this.enableMessage();
    };

    Portal.prototype.enableMessage = function(){
        var self = this;
        $(window).on('message',function(message){
            var data = message.originalEvent.data;
            if(data.type && data.type == "portal"){
                var method = data.method;
                var args = data.arguments;
                self[method].apply(self,args);
            }
        });
    };





    Portal.prototype.gotoPage = function(options){
        var id = options.id;
        if(this.pagesNode.find('.page-on-center').attr('data-page-id') == id){
           return false;
        }
        if(typeof id == 'undefined'){
            return false;
        }
        return this._showPage({
            id:id
        });
    };

    Portal.prototype.goBackPage = function(options){
        var centerPageNode = this.pagesNode.find('.page-on-center');
        var id =  centerPageNode.attr('data-page-id');
        var pid = centerPageNode.attr('data-page-pid');
        if(typeof pid == 'undefined'){
            return false;
        }
        var promise = this.gotoPage({id:pid});
        var self = this;
        return promise.done(function(){
            self._removePage({
                id:id
            });
        });
    };

    Portal.prototype.loadPage = function(options){
        var id = options.id || this.id++;
        var url = options.url;
        if(!url){
            return false;
        }
        var pid = options.pid || this.pagesNode.find('.page-on-center').attr('data-page-id');
        var title = options.title || url;
        var type = options.type || 'iframe';
        var currentPageNode = $(pageTpl).clone();
        this._appendPage({
            pageNode:currentPageNode,
            type:type,
            url:url,
            id:id,
            pid:pid
        });
        return this._showPage({
            id:id
        });
    };
    Portal.prototype._appendPage = function(options){
        var pageNode = options.pageNode;
        var type = options.type;
        var id = options.id;
        var pid = options.pid;
        var url = options.url;

        if(type == 'iframe'){
            var pageContentNode = pageNode.find('.page-content');
            $('<div class="iframe-fix"><iframe></iframe></div>').appendTo(pageContentNode).find('iframe').attr('src',url);
        }else if(type == 'compose'){

        }
        pageNode.attr({"data-page-id":id,"data-page-url":url,"data-page-pid":pid});
        this.pagesNode.append(pageNode);
    };


    Portal.prototype._removePage = function(options){
        var self = this;
        var id  = options.id;
        var targetPageNode = $('.page[data-page-id = "'+ id +'"]');
        var childPageNode = $('.page[data-page-pid = "'+ id +'"]');
        $.each(childPageNode,function(index,page){
            var childID = $(page).attr('data-page-id');
             self._removePage({
                 id:childID
             });
        });
        if(!targetPageNode.hasClass('page-on-center')){
            targetPageNode.remove();
        }
    };

    Portal.prototype._showPage = function(options){
        var self = this;
        var id  = options.id;
        var centerPageNode = this.rootNode.find('.page-on-center');
        var targetPageNode = $('.page[data-page-id = "'+ id +'"]');
        var direction = targetPageNode.hasClass('page-on-left')?'right':'left';
        var side = direction=='right'?'left':'right';
        centerPageNode.addClass('page-from-center-to-' + direction).removeClass('page-on-center');
        targetPageNode.addClass('page-from-'+ side +'-to-center').removeClass('page-on-'+ side);
        var centerIndex = self.pagesNode.find('.page').index(targetPageNode);
        var dtd = $.Deferred()
        targetPageNode.animationEnd(function(){
            targetPageNode.addClass('page-on-center').removeClass('page-from-'+ side +'-to-center');
            $.each(self.pagesNode.find('.page'),function(key,value){
                var valueNode = $(value);
                if(key < centerIndex){
                    valueNode.removeClass('page-on-right').addClass('page-on-left');
                }else if(key > centerIndex){
                    valueNode.removeClass('page-on-left').addClass('page-on-right');
                }
            });
            centerPageNode.addClass('page-on-'+ direction).removeClass('page-from-center-to-' + direction);
            dtd.resolve();
        });
        return dtd.promise();
    };

    var rootNode = $('.view');
    var portal = new Portal({
        rootNode:rootNode
    });

    return portal;
});